package cn.mdx.adminservice;


import cn.mdx.common.beans.User;
import cn.mdx.common.utils.CheckUtil;
import cn.mdx.mapper.AdminMapper;
import cn.mdx.mapper.CollegeMapper;
import cn.mdx.mapper.ResultMapper;
import cn.mdx.mapper.VoMapper;
import cn.mdx.pojo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class RootService {
    private static final Logger logger = LoggerFactory.getLogger(RootService.class);
    @Autowired
    public AdminMapper adminMapper;

    @Autowired
    public ResultMapper resultMapper;

    @Autowired
    public CollegeMapper collegeMapper;


    @Autowired
    private VoMapper voMapper;

    //查询专利信息
    public PageInfo<Patent> getAll(Integer cid,Integer pageno){
        if (pageno==null||pageno<=0) {
            pageno=1;
        }
        logger.info("[管理员查询专利信息]");
        PageHelper.startPage(pageno,6);
        List<Patent> p=adminMapper.getAllPatent(cid);
        for(Patent p1:p){
            System.out.println(p1.getPid()+"=="+p1.getUsername());
        }
        System.out.println("数据量："+p.size());
        return new PageInfo<Patent>(p);
    }



    public PageInfo<Terrace> getAllTerrace(Integer cid,Integer pageno){
        System.out.println("cid:"+cid+"====pageno:"+pageno);
        if (pageno==null||pageno<=0) {
            pageno=1;
        }
        logger.info("[管理员查询平台资源信息]");
        PageHelper.startPage(pageno,6);
        List<Terrace> terraces=adminMapper.getAllTerrace(cid);
        return new PageInfo<Terrace>(terraces);
    }

    /**
     * 管理员查询团队信息
     * @param session
     * @param cid
     * @param pageno
     * @return 团队信息
     */
    public PageInfo<Team> getAllTeam(HttpSession session,Integer cid,Integer pageno){
        if (pageno==null||pageno<=0) {
            pageno=1;
        }
        logger.info("[管理员查询团队资源信息]");
        User user=(User) session.getAttribute("user");
        logger.info("[管理员查询团队资源信息，参数[id:{},username{}]]",user.getId(),user.getUsername());
        PageHelper.startPage(pageno,6);
        List<Team> teams=adminMapper.getAllTeam(cid);
        logger.info("[管理员查询团队资源信息成功]");
        return new PageInfo<Team>(teams);
    }


    /**
     * 查询软件著作权信息
     * @param cid
     * @param pageno
     * @param uid
     * @return 软件著作权信息
     */
    public PageInfo<Software> getAllSoftware(Integer cid,Integer pageno,Integer uid){
        if (pageno==null||pageno<=0) {
            pageno=1;
        }
        logger.info("[管理员查询软件著作权信息]");
        logger.info("[管理员查询软件著作权信息，参数[id:{}]]",uid);
        PageHelper.startPage(pageno,6);
        List<Software> softwares=adminMapper.getAllSoftware(cid);
        logger.info("[管理员查询软件著信息成功]");
        return new PageInfo<Software>(softwares);
    }


    /**
     * 获取所有对接成果信息
     * @param pageno
     * @return
     */
    public PageInfo<Result> getAllResult(HttpSession session,Integer pageno){
        User user=(User)session.getAttribute("user");
        Integer id=user.getId();
        Admin admin=adminMapper.selectByPrimaryKey(id);
        logger.info("[管理员查询对接成果信息]");
        PageHelper.startPage(pageno,6);
        List<Result> resultList=resultMapper.selectByCid(admin.getCid());
        logger.info("[管理员查技术需求方信息成功]");
        return new PageInfo<Result>(resultList);
    }


    /**
     * 获取学院信息
     * @return
     */
    public List<College> getAllCollege(){
        return collegeMapper.getColleges();
    }


    /**
     * 查询统计数据
     * @param cid 部门id，查询条件，可以为空
     * @param some 1：年:2：季:3：月
     * @return 统计数据
     */
    public ArrayList<StatisticsVO> getBySome(Integer cid, Integer some){
        CheckUtil.notNull(some,"参数为空");
        logger.info("[获取统计数据，参数[cid:{},Some:{}]]",cid,some);
        switch (some){
            case 1: return voMapper.getByYear(cid);
            case 2: return voMapper.getByQuarter(cid);
            case 3: return voMapper.getByMonth(cid);
            default:return voMapper.getByYear(cid);
        }
    }

}
